package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.LocationMapper;
import com.iretailer.report.model.Location;
import com.iretailer.report.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/1/24 11:35
 * @Description:
 */
@Service
public class LocationService extends ServiceImpl<LocationMapper, Location> {

    @Autowired
    private LocationMapper locationMapper;

    /**
     * 获取 地理位的 树形 结构
     */
//    @Cacheable(value = "getLocationTree")
    public List<Location> getLocationTree() {
        List<Location> findList = list(new QueryWrapper<Location>().orderByAsc("Ranked"));
        return convertTree(findList, "-");
    }

    public List<Location> convertTree(List<Location> locations, String parentId) {
        List<Location> result = new ArrayList<>();
        locations.forEach(it -> {
            if (it.getParentId() != null && (it.getParentId()).equals(parentId)) {
                List<Location> children = convertTree(locations, it.getId());
                it.setChildren(children);
                result.add(it);

            }
        });
        return result;
    }

    public PageResult<Location> selectByParentIdPage(String parentId, QueryOption queryOption) {
        Page page = new Page<Location>(1, Long.MAX_VALUE);
        QueryWrapper<Location> queryWrapper = new QueryWrapper<>();
        if (parentId != null) {
            queryWrapper.eq("parentId", parentId);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByAsc("ranked");
        IPage pageInfo = locationMapper.selectPage(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    /**
     * 根据 parentId 查询出 location
     */
    public List<Location> selectLocationByParentId(String parentId) {
        return locationMapper.selectLocationByParentId(parentId);
    }

    /**
     * 新增
     * 更新
     */
    public boolean saveOrUpdate(Location location, String usreName) {
        location.setUpdateTime(LocalDateTime.now());
        location.setUpdateUser(usreName);
        if (StringUtil.notEmpty(location.getId())) {
            locationMapper.updateById(location);
            return true;
        } else {
            locationMapper.insert(location);
            return true;
        }
    }

    public boolean deleteByIds(List<String> ids) {
        if (ids != null && ids.size() > 0) {
            for (String id : ids) {
                List<Location> find = list(new QueryWrapper<Location>().lambda().eq(Location::getParentId, id));
                if (find != null && find.size() > 0) {
                    return false;
                }
            }
            return removeByIds(ids);
        } else {
            return false;
        }
    }
}
